package org.example.springboot;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;

@RequestMapping("/test/test1")
@RestController
public class Test1 {
    @RequestMapping(value = "/people/p",method = RequestMethod.GET) //设置URL
    public String people() {
        return "people";
    }

    @RequestMapping("/t1")
    public String t1(String name) {
        return "已接收到数据：" + name;
    }

    @RequestMapping("/t2")
    public String t2(String name, Integer age, String other) {
        return "name = " + name + ", age = " + age;
    }

    @RequestMapping("/newUrl")
    public String t3(String name, Integer age, String other) {
        return name + age;
    }

    @RequestMapping("/t4")
    public String t4(Param param) {
        return param.toString();
    }

    @RequestMapping("/t5")
    public String t5(@RequestParam(value = "name",required = false) String userName, Integer age) {
        return "userName = " + userName + ", age = " + age;
    }

    @RequestMapping("/t6")
    public String t6(String[] arr) {
        return Arrays.toString(arr) + "size = " + arr.length;
    }

    @RequestMapping("/t7")
    public String t7(@RequestParam("list") List<String> list) {
        if (list == null) return "list为空";
        return list.toString();
    }
    @RequestMapping("/t8")
    public String t8(@RequestBody Param param) {
        return param.toString();
    }

    @RequestMapping("/t9/{number}")
    public String t9(@PathVariable Integer number) {
        return "number = " + number;
    }


    @RequestMapping("/t10/{str}/{number}")
    public String t10(@PathVariable(value = "str",required = false) String ppp, @PathVariable(value = "number",required = false) Integer number) {
        return "str = " + ppp + ",number = " + number;
    }

    @RequestMapping("/t11")
    public String t11(@RequestPart MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename();
        file.transferTo(new File("D:/tmp/" + fileName));
        return "上传成功" + file.getOriginalFilename();
    }
}
